import 'package:flutter/material.dart';
import 'package:hospitable_rental/models/room_detail.dart';
import 'package:hospitable_rental/pages/home/home/home_advisory.dart';
import 'package:hospitable_rental/pages/room/publish/room_applicance.dart';
import 'package:hospitable_rental/widgets/common_swiper.dart';
import 'package:hospitable_rental/widgets/common_tags.dart';
import 'package:hospitable_rental/widgets/form_item/common_title.dart';
import 'package:share/share.dart';

class RoomDetailPage extends StatefulWidget {
  final String? roomId;

  const RoomDetailPage({Key? key, this.roomId}) : super(key: key);

  @override
  _RoomDetailPageState createState() => _RoomDetailPageState();
}

class _RoomDetailPageState extends State<RoomDetailPage> {
  RoomDetail? _roomDetail;
  bool _isLike = false;
  bool _showAllText = false;
  bool _showTextTool = false;

  @override
  void initState() {
    _roomDetail = defaultRoomDetail;
    super.initState();
  }

  Widget _getPriceWidget() {
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0, bottom: 10.0),
      child: Row(
        crossAxisAlignment: CrossAxisAlignment.end,
        children: [
          Text(
            (_roomDetail?.price ?? 0.0).toString(),
            style: TextStyle(
              fontSize: 20.0,
              color: Colors.red,
            ),
          ),
          Text(
            " 元/月 (押一付三)",
            style: TextStyle(
              fontSize: 14.0,
              color: Colors.deepOrangeAccent,
            ),
          )
        ],
      ),
    );
  }

  Widget _getTagsWidget() {
    return Container(
      padding: EdgeInsets.only(left: 10.0, right: 10.0, bottom: 10.0),
      child: Wrap(
        spacing: 4.0,
        children: _roomDetail!.tags!
            .map(
              (item) => CommonTags(item),
            )
            .toList(),
      ),
    );
  }

  Widget _getInfoWidget() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.0, vertical: 10.0),
      child: Wrap(
        spacing: 10.0,
        runSpacing: 10.0,
        children: [
          _infoItemWidget("面积：${_roomDetail?.size.toString()}m²"),
          _infoItemWidget("楼层：${_roomDetail?.floor}"),
          _infoItemWidget("房型：${_roomDetail?.roomType}"),
          _infoItemWidget("装修：精装"),
        ],
      ),
    );
  }

  Widget _getDescWidget() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 10.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Text(
            _roomDetail?.subTitle ?? "暂无房屋概括",
            maxLines: _showAllText ? null : 4,
          ),
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: [
              _showTextTool
                  ? GestureDetector(
                      onTap: () => setState(() => _showAllText = !_showAllText),
                      child: Row(
                        children: [
                          Text(_showAllText ? "收起" : "展开"),
                          Icon(
                            _showAllText ? Icons.keyboard_arrow_up : Icons.keyboard_arrow_down,
                            color: Colors.grey,
                          ),
                        ],
                      ),
                    )
                  : Container(),
              Text("举报"),
            ],
          ),
        ],
      ),
    );
  }

  Widget _infoItemWidget(String title) {
    return Container(
      width: (MediaQuery.of(context).size.width - 10 * 3) / 2,
      child: Text(title),
    );
  }

  @override
  Widget build(BuildContext context) {
    if (_roomDetail == null) return Container();

    _showTextTool = (_roomDetail?.subTitle ?? "").length > 100;
    return Scaffold(
      appBar: AppBar(
        title: Text(_roomDetail?.title ?? "房屋详情"),
        actions: [
          IconButton(
            icon: Icon(Icons.share),
            onPressed: () => Share.share('https://www.baidu.com'),
          )
        ],
      ),
      body: Stack(
        children: [
          ListView(
            children: [
              CommonSwiper(images: _roomDetail?.houseImgs ?? []),
              CommonTitle(_roomDetail?.title ?? ""),
              _getPriceWidget(),
              if (_roomDetail?.tags != null) _getTagsWidget(),
              Divider(
                height: 1.0,
                color: Colors.grey[500],
                indent: 10.0,
                endIndent: 10.0,
              ),
              _getInfoWidget(),
              CommonTitle("房屋配置"),
              if (_roomDetail?.applicances != null)
                RoomApplicanceList(
                  list: _roomDetail!.applicances!,
                ),
              CommonTitle("房屋概括"),
              _getDescWidget(),
              CommonTitle("猜你喜欢"),
              AdvisoryWidget(showTitle: false),
              Container(height: 100.0),
            ],
          ),
          Positioned(
            bottom: 0.0,
            width: MediaQuery.of(context).size.width,
            height: 100.0,
            child: Container(
              padding: EdgeInsets.only(left: 10.0, right: 20.0, top: 10.0, bottom: 10.0),
              decoration: BoxDecoration(color: Colors.grey[200]),
              child: Row(
                children: [
                  GestureDetector(
                    behavior: HitTestBehavior.translucent,
                    onTap: () => setState(() => _isLike = !_isLike),
                    child: Container(
                      width: 50.0,
                      height: 50.0,
                      child: Column(
                        children: [
                          Icon(
                            _isLike ? Icons.star : Icons.star_border,
                            size: 24.0,
                            color: _isLike ? Colors.green : Colors.black54,
                          ),
                          Text(
                            _isLike ? "已收藏" : "收藏",
                            style: TextStyle(fontSize: 12.0),
                          ),
                        ],
                      ),
                    ),
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {},
                      child: Container(
                        height: 50.0,
                        decoration: BoxDecoration(
                          color: Colors.cyan,
                          borderRadius: BorderRadius.circular(6.0),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          "联系房东",
                          style: TextStyle(
                            fontSize: 16.0,
                            fontWeight: FontWeight.w500,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  ),
                  SizedBox(width: 10.0),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {},
                      child: Container(
                        height: 50.0,
                        decoration: BoxDecoration(
                          color: Colors.green,
                          borderRadius: BorderRadius.circular(6.0),
                        ),
                        alignment: Alignment.center,
                        child: Text(
                          "预约看房",
                          style: TextStyle(
                            fontSize: 16.0,
                            fontWeight: FontWeight.w500,
                            color: Colors.white,
                          ),
                        ),
                      ),
                    ),
                  )
                ],
              ),
            ),
          ),
        ],
      ),
    );
  }
}
